package com.leetcode.partition14;

import java.util.Arrays;

/**
 * @author rkc
 * @date 2022/9/9 9:46
 */
public class LC1387将整数按权重排序 {

    private int[][] steps = new int[1010][2];
    private int size = 0;

    public int getKth(int lo, int hi, int k) {
        for (int i = lo; i <= hi; i++) {
            steps[size][0] = i;
            steps[size][1] = get(i);
            size++;
        }
        Arrays.sort(steps, 0, size, (o1, o2) -> {
            if (o1[1] != o2[1]) return Integer.compare(o1[1], o2[1]);
            return Integer.compare(o1[0], o2[0]);
        });
        return steps[k - 1][0];
    }

    private int get(int x) {
        int res = 0;
        while (x != 1) {
            if (x % 2 == 0) x /= 2;
            else x = x * 3 + 1;
            res++;
        }
        return res;
    }
}
